package asn.match.dao;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import asn.match.model.RessourceContainer;

@Service("ressource_container")
@PersistenceContext(type = PersistenceContextType.EXTENDED)
@Transactional
public class RessourceContainerDaoImpl implements RessourceContainerDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addRessourceContainer(RessourceContainer ressourceContainer) {
		Date date = new Date();
		Timestamp created_at = new Timestamp(date.getTime());
		ressourceContainer.setCreatedAt(created_at);
		this.sessionFactory.getCurrentSession().save(ressourceContainer);
	}

	@Override
	public void updateRessourceContainer(RessourceContainer ressourceContainer) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(ressourceContainer);
	}

	@Override
	public void deleteRessourceContainer(RessourceContainer ressourceContainer) {
		this.sessionFactory.getCurrentSession().delete(ressourceContainer);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RessourceContainer> findAllRessourceContainers() {
		return (List<RessourceContainer>) this.sessionFactory.getCurrentSession().createQuery("from RessourceContainer ressource_container").list();
	}

	@Override
	public RessourceContainer findRessourceContainerById(Integer ressourceContainerId) {
		return (RessourceContainer) this.sessionFactory.getCurrentSession().get(RessourceContainer.class, ressourceContainerId);
	}

}
